package psn.hui.sys.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Properties;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

public class AppFileUtils {
	/**
	 * 得到文件上传的路径
	 */
	public static String PATH="E:/upload/";
	static {
		InputStream stream = AppFileUtils.class.getClassLoader().getResourceAsStream("file.properties");
		Properties properties = new Properties();
		try {
			properties.load(stream);
			PATH = properties.getProperty("path")+"/";
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 文件下载
	 * @param response
	 * @param path
	 * @param oldName
	 * @return
	 */
	public static ResponseEntity<Object> downloadFile(HttpServletResponse response, String path, String oldName){
		File file = new File(AppFileUtils.PATH, path);
		if(file.exists() && file.isFile()) {
			try {
				try {
					oldName = URLEncoder.encode(oldName, "UTF-8");
				} catch (Exception e) {
					e.printStackTrace();
				}
				//把file转成一个bytes
				byte[] bytes = FileUtils.readFileToByteArray(file);
				HttpHeaders header = new HttpHeaders();
				//封装响应内容类型
				header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
				//设置下载的文件的名称
				header.setContentDispositionFormData("attachment", oldName);
				//创建ResponEntity对象
				ResponseEntity<Object> entity = 
						new ResponseEntity<Object>(bytes, header, HttpStatus.CREATED);
				return entity;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}else {
			PrintWriter out;
			try {
				out = response.getWriter();
				out.write("文件不存在");
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
	}
	/**
	 * 根据相对路径删除硬盘上的文件
	 * @param path
	 */
	public static void deleteFileUsePath(String path) {
		try {
			String realPath = PATH+path;
			//根据文件
			File file = new File(realPath);
			if(file.exists()) {
				file.delete();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	/**
	 * 	移除上传的临时文件后缀，成为正式文件
	 * @param path		临时文件路径
	 * @param suffix	临时文件后缀
	 */
	public static String removeTempFileSuffix(String path,String suffix) {
		try {
			String realPath = PATH+path;
			String newName = path;
			if(path != null && path.length()>suffix.length()) {
				newName = path.replace(suffix, "");
			}
			String newPath = PATH+newName;
			//根据文件
			File file = new File(realPath);
			if(file.exists() && file.isFile()) {
				file.renameTo(new File(newPath));
			}
			return newName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path;
	}
	/**
	 * 	将正式文件加上临时文件后缀
	 * @param path		文件路径
	 * @param suffix	临时文件后缀
	 */
	public static String addTempFileSuffix(String path,String suffix) {
		try {
			String realPath = PATH+path;
			String newName = path+suffix;
			String newPaht = PATH+newName;
			//根据文件
			File file = new File(realPath);
			if(file.exists() && file.isFile()) {
				file.renameTo(new File(newPaht));
			}
			return newName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path;
	}
}
